home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / IR Man™ / IRMan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-07  |  11.9 KB  |  563 lines  |  [TEXT/KAHL]

  1.  
  2. #include "2BufRecordToBufCmd.h"
  3. #include "atob.h"
  4.  
  5. #ifdef IRDEBUG
  6. #include "stdio.h"
  7. #endif
  8.  
  9. #include "CD ROM.h"
  10. #include "PlayMovie.h"
  11. #include "KillApp.h"
  12. #include <Shutdown.h>
  13.  
  14. struct IRStateStruct {
  15.     long    oldValue;
  16.     short    quitFlag;
  17.     short    CDPauseState;   /* 0 = CD is paused, 1 = CD is playing */
  18.     short    CDPlayState;
  19.     short    CDScanState;
  20.     ProcessSerialNumber    IRProc;
  21. };
  22. typedef struct IRStateStruct IRStateStruct;
  23.  
  24. void InitStuff(void);
  25. void MainEventLoop(void);
  26. void ProcessIR(IRStateStruct *IRState);
  27. void DoVideoIR(long theValue,IRStateStruct *IRState);
  28. void DoProcessIR(long theValue,IRStateStruct *IRState);
  29. void IRSwitchLayer(short forward, IRStateStruct *IRState);
  30. short IsMyWindow(WindowPtr theWindow);
  31. void OffsetWindow(WindowPtr theWindow, short h, short v);
  32. void InitAEStuff(void);
  33. void DoHighLevel(EventRecord *AERecord);
  34. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  35. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  36. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  37. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  38.  
  39.  
  40. extern     WindowPtr        myWindow[MAXWINDOWS];
  41. short    quitFlag = 0;
  42.  
  43.  
  44. void
  45. main()
  46. {    
  47.  
  48.     InitStuff();
  49.  
  50.     TestTheSystem ();    // test the system to make sure the app will work
  51.  
  52.     InitAEStuff();
  53.  
  54.     InitPlayMovie();
  55.  
  56.     InitSoundStuff();
  57.     StartRecord();
  58. #ifdef IRDEBUG
  59.     printf("Start!\n");
  60. #endif
  61.                 
  62.     MainEventLoop();
  63.     
  64.     SoundCleanUpAndQuit();
  65.     ClosePlayMovie();
  66. }
  67.  
  68. void
  69. InitStuff()
  70. {
  71.     OSErr        err;
  72.     
  73.     MoreMasters();
  74.     MoreMasters();
  75.     MoreMasters();
  76.  
  77.     InitGraf(&thePort);
  78.     InitFonts();
  79.     InitWindows();
  80.     InitMenus();
  81.     TEInit();
  82.     InitDialogs(0L);
  83.     InitCursor();
  84.         
  85.     FlushEvents( everyEvent, 0 );
  86.     MaxApplZone();
  87.     InitCursor();        
  88. }
  89.  
  90. void
  91. ProcessIR(IRStateStruct *IRState)
  92. {
  93.     long    theValue;
  94.     short    sameVal;
  95.     
  96.     theValue = SendSoundData();
  97.  
  98.     if (theValue == IRState->oldValue) {
  99.         theValue = 0;
  100.     }
  101.     else {
  102.         IRState->oldValue = theValue;
  103.         if (IRState->CDScanState == 1) {
  104.             APause(0, GetDrvRefNum(-2));
  105.             IRState->CDScanState = 0;
  106.             IRState->CDPauseState = 1;
  107.         }
  108.     }
  109.     
  110.     if (theValue > 0) {
  111. #ifdef IRDEBUG
  112.         printf("Value: %ld\n", theValue);
  113. #endif
  114.  
  115.  
  116.         if (theValue == 2232) {
  117.             AStop(0,GetDrvRefNum(-2));
  118.             IRState->CDPlayState = 0;
  119.             IRState->CDPauseState = 1;
  120.         }
  121.             
  122.         if (theValue == 2226) 
  123.             if (!(IRState->CDPlayState)) {
  124.                 Play(1,-2);
  125.                 IRState->CDPlayState = 1;
  126.             }
  127.             else
  128.             if (!(IRState->CDPauseState)) {
  129.                 APause(IRState->CDPauseState, GetDrvRefNum(-2));
  130.                 IRState->CDPauseState = !(IRState->CDPauseState);
  131.             }
  132.             
  133.         if ((theValue >= 2176) && (theValue <= 2184)) {
  134.             Play (theValue - 2175, -2);
  135.             IRState->CDPlayState = 1;
  136.         }
  137.         if ((theValue >= 2208) && (theValue <= 2214)) {
  138.             Play (theValue - 2208 + 10, -2);
  139.             IRState->CDPlayState = 1;
  140.         }
  141.         if ((theValue >= 2192) && (theValue <= 2193)) {
  142.             Play (theValue - 2192 + 17, -2);
  143.             IRState->CDPlayState = 1;
  144.         }
  145.         if (theValue == 2199) {
  146.             Play (19, -2);
  147.             IRState->CDPlayState = 1;
  148.         }
  149.         if (theValue == 2217) {
  150.             Play (20, -2);
  151.             IRState->CDPlayState = 1;
  152.         }
  153.         
  154.         if (theValue == 2233) {
  155.             APause(IRState->CDPauseState, GetDrvRefNum(-2));
  156.             IRState->CDPauseState = !(IRState->CDPauseState);
  157.         }
  158.     
  159.         if (theValue == 2198) {
  160.             AStop(0,GetDrvRefNum(-2));
  161.             IRState->CDPlayState = 0;
  162.             IRState->CDPauseState = 1;
  163.             CDEject(-2);
  164.         }
  165.         
  166.         if (theValue == 2228) {
  167.             if (AScan(0, GetDrvRefNum(-2)) == noErr) {
  168.                 IRState->CDScanState = 1;
  169.             }
  170.         }
  171.             
  172.         if (theValue == 2227) {
  173.             if (AScan(1, GetDrvRefNum(-2)) == noErr) {
  174.                 IRState->CDScanState = 1;
  175.             }
  176.         }            
  177.         
  178.         if ((theValue == 2225) && (IRState->CDPlayState == 1))
  179.             ATrackSkip(1, GetDrvRefNum(-2));
  180.         
  181.         if ((theValue == 2224) && (IRState->CDPlayState == 1))
  182.             ATrackSkip(0, GetDrvRefNum(-2));
  183.  
  184.         DoVideoIR(theValue,IRState);
  185.         
  186.         DoProcessIR(theValue,IRState);
  187.         
  188.     }
  189. }
  190.  
  191. void
  192. DoVideoIR(long theValue,IRStateStruct *IRState) {
  193.     
  194.     switch (theValue) {
  195.         case 144:
  196.             BringToMooVFront(1);
  197.         break;
  198.         case 145:
  199.             BringToMooVFront(0);
  200.         break;
  201.         case 1474:
  202.             if (IsMyWindow(FrontWindow()))
  203.                 OffsetWindow(FrontWindow(), 0, -20);
  204.             IRState->oldValue = 0;
  205.         break;
  206.         case 1475:
  207.             if (IsMyWindow(FrontWindow()))
  208.                 OffsetWindow(FrontWindow(), 0, 20);
  209.             IRState->oldValue = 0;
  210.         break;
  211.         case 1506:
  212.             if (IsMyWindow(FrontWindow()))
  213.                 OffsetWindow(FrontWindow(), -20, 0);
  214.             IRState->oldValue = 0;
  215.         break;
  216.         case 1505:
  217.             if (IsMyWindow(FrontWindow()))
  218.                 OffsetWindow(FrontWindow(), 20, 0);
  219.             IRState->oldValue = 0;
  220.         break;
  221.         case 1434:   /* VCR Play  */
  222.             IRMooVPlay(65536);
  223.         break;
  224.         case 1432:   /* VCR Stop  */
  225.             IRMooVPlay(0);
  226.         break;
  227.         case 1435:   /* VCR Rew  */
  228.             IRMooVPlay(-65536 * 2);
  229.         break;
  230.         case 1436:   /* VCR FF  */
  231.             IRMooVPlay(65536 * 2);
  232.         break;
  233.         case 1448:   /* VCR Rew Search */
  234.             IRMooVPlay(-32768);
  235.         break;
  236.         case 1449:   /* VCR FF Search */
  237.             IRMooVPlay(32768);
  238.         break;
  239.         case 1533:   /* VCR High Speed Rewind */
  240.             IRMooVPlay(-65536 * 20);
  241.         break;
  242.         case 1440:   /* VCR Pause in Jog Mode  */
  243.             IRMooVPlay(0);
  244.         break;
  245.         case 1457:   /* VCR step Forward in Jog Mode  */
  246.             IRMooVStep(0);
  247.             IRState->oldValue = 0;
  248.         break;
  249.         case 1456:   /* VCR step Back in Jog Mode  */
  250.             IRMooVStep(-1);
  251.             IRState->oldValue = 0;
  252.         break;
  253.         case 146:   /* Volume ++  */
  254.             IRMooVVolume(1);
  255.             IRState->oldValue = 0;
  256.         break;
  257.         case 147:   /* Volume --  */
  258.             IRMooVVolume(0);
  259.             IRState->oldValue = 0;
  260.         break;
  261.         case 149:   /* Close Window  */
  262.             doCloseCommand();
  263.         break;
  264.     }
  265. }
  266.  
  267.  
  268. void
  269. DoProcessIR(long theValue,IRStateStruct *IRState) {
  270.     ProcessSerialNumber        frontProc;
  271.     Boolean                    equalProc;
  272.     ProcessInfoRec            procInfo;
  273.     FSSpec                    extraFSSpec;
  274.     Str255                    extraString;
  275.  
  276.     procInfo.processInfoLength = sizeof(ProcessInfoRec);
  277.     procInfo.processName = extraString;
  278.     procInfo.processAppSpec = &extraFSSpec;
  279.  
  280.     switch (theValue) {
  281.         case 1424:
  282.             IRSwitchLayer(1, IRState);
  283.         break;
  284.         case 1425:
  285.             IRSwitchLayer(0, IRState);
  286.         break;
  287.         case 1429:
  288.             GetFrontProcess(&frontProc);
  289.             SameProcess(&IRState->IRProc, &frontProc, &equalProc);
  290.             if (equalProc)
  291.                 quitFlag = 1;
  292.             else {
  293.                 if (GetProcessInformation(&frontProc, &procInfo) == noErr ) {
  294.                     if (procInfo.processType == 'FNDR') {
  295.                         /* ShutDwnPower(); */
  296.                         SysBeep(5);
  297.                     }
  298.                     else {
  299.                         QuitAnApp(&frontProc);
  300.                     }
  301.                 }
  302.             }
  303.         break;
  304.     }
  305. }
  306.  
  307. void
  308. IRSwitchLayer(short forward, IRStateStruct *IRState) {
  309.     ProcessSerialNumber        frontProc, currProc, nextProc, prevProc;
  310.     OSErr                     err;
  311.     Boolean                    equalProc;
  312.     short                    foundOne = 0;
  313.     ProcessInfoRec            procInfo;
  314.     FSSpec                    extraFSSpec;
  315.     Str255                    extraString;
  316.     
  317.     err = GetFrontProcess(&frontProc);
  318.     if (err != noErr)
  319.         return;
  320.     
  321.     procInfo.processInfoLength = sizeof(ProcessInfoRec);
  322.     procInfo.processName = extraString;
  323.     procInfo.processAppSpec = &extraFSSpec;
  324.     
  325.     currProc.highLongOfPSN = 0;
  326.     currProc.lowLongOfPSN = kNoProcess;
  327.     prevProc.highLongOfPSN = 0;
  328.     prevProc.lowLongOfPSN = kNoProcess;
  329.     
  330.     while ((GetNextProcess(&currProc) == noErr) && (!foundOne)) {
  331.         if (GetProcessInformation(&currProc, &procInfo) == noErr ) {
  332.             if ((!(procInfo.processMode & modeDeskAccessory)) && 
  333.                 (!(procInfo.processMode & modeOnlyBackground))) {
  334.                 
  335.                 SameProcess(&currProc, &frontProc, &equalProc);
  336.                 if (equalProc) {
  337.                     if (forward) {
  338.                         if (GetNextProcess(&currProc) == noErr) {
  339.                             nextProc.highLongOfPSN = currProc.highLongOfPSN;
  340.                             nextProc.lowLongOfPSN = currProc.lowLongOfPSN;
  341.                             foundOne = 1;
  342.                         }
  343.                         else {
  344.                             currProc.highLongOfPSN = 0;
  345.                             currProc.lowLongOfPSN = kNoProcess;
  346.                             GetNextProcess(&currProc);
  347.                             nextProc.highLongOfPSN = currProc.highLongOfPSN;
  348.                             nextProc.lowLongOfPSN = currProc.lowLongOfPSN;
  349.                             foundOne = 1;
  350.                         }
  351.                     }
  352.                     else {
  353.                         if (prevProc.lowLongOfPSN != kNoProcess) {
  354.                             nextProc.highLongOfPSN = prevProc.highLongOfPSN;
  355.                             nextProc.lowLongOfPSN = prevProc.lowLongOfPSN;
  356.                             foundOne = 1;
  357.                         }
  358.                         else {
  359.                             while (GetNextProcess(&currProc) == noErr) {
  360.                                 nextProc.highLongOfPSN = currProc.highLongOfPSN;
  361.                                 nextProc.lowLongOfPSN = currProc.lowLongOfPSN;
  362.                             }
  363.                             foundOne = 1;
  364.                         }
  365.                     }
  366.                 }
  367.                 prevProc.highLongOfPSN = currProc.highLongOfPSN;
  368.                 prevProc.lowLongOfPSN = currProc.lowLongOfPSN;
  369.             }
  370.         }
  371.     }
  372.     
  373.     if (foundOne)
  374.         SetFrontProcess(&nextProc);
  375. }
  376.  
  377. void
  378. MainEventLoop(void)
  379. {
  380.     short            i;
  381.     char            key;
  382.     Boolean            track;
  383.     long            growResult;
  384.     EventRecord     myEvent;
  385.     WindowPtr        whichWindow;
  386.     short            yieldTime;
  387.     short            isEvent;
  388.     IRStateStruct    IRState;
  389.     AudioStateStruct    aState;
  390.     
  391.     IRState.oldValue = 0;
  392.     IRState.quitFlag = 0;
  393.     IRState.CDPauseState = 1;   /* 0 = CD is paused, 1 = CD is playing */
  394.     IRState.CDPlayState = 0;
  395.     IRState.CDScanState = 0;
  396.     GetCurrentProcess(&(IRState.IRProc));
  397.  
  398.     if (!AStatus(GetDrvRefNum(-2), &aState)) {
  399.         switch (aState.aStatusFlags) {
  400.             case 0:
  401.                 IRState.CDPlayState = 1;
  402.             break;
  403.             case 1:
  404.                 IRState.CDPauseState = 0;
  405.             break;
  406.         }
  407.     }
  408.     
  409.     yieldTime = 0;
  410.     while (!quitFlag) {
  411.                 
  412.         isEvent = WaitNextEvent(everyEvent, &myEvent, yieldTime, nil);
  413.         
  414.         ProcessIR(&IRState);
  415.         
  416.         if (playMovies(&myEvent))
  417.             ;
  418.         else
  419.         if (isEvent) {
  420.         
  421.             switch (myEvent.what) {
  422.                 case mouseDown:
  423.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  424.                         case inSysWindow:
  425.                             SystemClick(&myEvent, whichWindow);
  426.                             break;
  427.                         case inMenuBar:
  428.                             doCommand(MenuSelect(myEvent.where));
  429.                             break;
  430.                         case inContent:
  431.                             SelectWindow(whichWindow);
  432.                             break;
  433.                         case inDrag:
  434.                             DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
  435.                             break;
  436.                         case inGrow:
  437.                             break;
  438.                         case inGoAway:
  439.                             track = TrackGoAway (whichWindow, myEvent.where);
  440.                             if (track)    doCloseCommand();
  441.                             break;
  442.                         case inZoomIn:
  443.                             break;
  444.                         case inZoomOut:
  445.                             break;
  446.                         default:
  447.                             break;
  448.                     }
  449.                     break;
  450.                 case keyDown:
  451.                 case autoKey:
  452.                     key = myEvent.message & charCodeMask;
  453.                     if ( myEvent.modifiers & cmdKey )
  454.                         if ( myEvent.what == keyDown )
  455.                             doCommand(MenuKey(key));
  456.                     break;
  457.                 case updateEvt:
  458.                     for (i = 0; i < MAXWINDOWS; i++)
  459.                         if ((WindowPtr)myEvent.message == myWindow[i]) {
  460.                             BeginUpdate((WindowPtr) myWindow[i]);
  461.                             EndUpdate((WindowPtr) myWindow[i]);
  462.                         }
  463.                     break;
  464.                 case diskEvt:
  465.                     break;
  466.                 case activateEvt:
  467.                     break;
  468.                 case app4Evt:
  469.                     break;
  470.                 case kHighLevelEvent:
  471.                     DoHighLevel(&myEvent);
  472.                     break;
  473.                 default:
  474.                     break;
  475.             }
  476.         }
  477.     }
  478. }
  479.  
  480. short
  481. IsMyWindow(WindowPtr theWindow) {
  482.  
  483.     short    i;
  484.     
  485.     for (i = 0; i < MAXWINDOWS; i++)
  486.         if (theWindow == myWindow[i])
  487.             return(1);
  488.     
  489.     return(0);
  490. }
  491.  
  492. void
  493. OffsetWindow(WindowPtr theWindow, short h, short v) {
  494.     Point    pt;
  495.     GrafPtr    thePort;
  496.     
  497.     GetPort(&thePort);
  498.     SetPort(theWindow);
  499.     pt.h = 0;
  500.     pt.v = 0;
  501.     
  502.     LocalToGlobal(&pt);
  503.     pt.h = pt.h + h;
  504.     pt.v = pt.v + v;
  505.     
  506.     MoveWindow(theWindow, pt.h, pt.v, true);
  507.     SetPort(thePort);
  508. }
  509.  
  510. void
  511. InitAEStuff(void)
  512. {
  513.     
  514.     AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,(EventHandlerProcPtr) AEOpenHandler,0,false);
  515.     AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,(EventHandlerProcPtr) AEOpenDocHandler,0,false);
  516.     AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,(EventHandlerProcPtr) AEPrintHandler,0,false);
  517.     AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,(EventHandlerProcPtr) AEQuitHandler,0,false);                                
  518. }
  519.  
  520.  
  521.  
  522. void DoHighLevel(EventRecord *AERecord)
  523. {
  524.     
  525.     AEProcessAppleEvent(AERecord);
  526.     
  527. }
  528.  
  529.  
  530.     /* This is the standard Open Application event.  */
  531.  
  532. pascal OSErr
  533. AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  534. {
  535.     return(noErr);
  536. }
  537.  
  538.  
  539. pascal OSErr
  540. AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  541. {
  542.     /* we have no docs, so no odoc events should come to us */
  543.  
  544.     return(errAEEventNotHandled);         
  545. }
  546.  
  547. pascal OSErr
  548. AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  549. {
  550.     /* we have no docs, so no pdoc events should come to us */
  551.     
  552.     return(errAEEventNotHandled);
  553. }
  554.  
  555.  
  556. pascal OSErr
  557. AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  558. {
  559.     
  560.     quitFlag = 1;
  561.     return(noErr);
  562. }
  563.